package com.cinema.entity.user;

import com.cinema.service.UserService;
import com.cinema.util.InputUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Scanner;

/**
 * 管理员用户，负责系统用户管理
 */
@Getter
@Setter
@AllArgsConstructor
public class AdminUser extends AbstractUser implements Serializable {
    private static final long serialVersionUID = 1L;
    private final Scanner scanner = new Scanner(System.in);
    private final UserService userService = new UserService();

    public AdminUser(String userId, String username, String password, LocalDateTime createTime, String userType, String phone) {
        super(userId, username, password, createTime, userType, phone);
    }

    @Override
    public void showMenu() {
        while (true) {
            System.out.println("\n===== 管理员菜单 =====");
            System.out.println("1. 修改个人密码");
            System.out.println("2. 用户管理（增删改查）");
            System.out.println("3. 退出登录");
            System.out.print("请选择操作[1-3]：");

            int choice = InputUtil.getValidInt();
            switch (choice) {
                case 1:
                    handleChangePassword();
                    break;
                case 2:
                    handleUserManagement();
                    break;
                case 3:
                    System.out.println("✅ 已退出登录");
                    return;
                default:
                    System.out.println("❌ 无效选择，请重新输入");
            }
        }
    }

    // 修改个人密码
    private void handleChangePassword() {
        System.out.print("请输入旧密码：");
        String oldPwd = scanner.next();
        System.out.print("请输入新密码：");
        String newPwd = scanner.next();

        if (super.changePassword(oldPwd, newPwd)) {
            System.out.println("✅ 密码修改成功");
            userService.updateUser(this); // 保存修改
        } else {
            System.out.println("❌ 旧密码错误，修改失败");
        }
    }

    // 用户管理入口
    private void handleUserManagement() {
        System.out.println("\n===== 用户管理 =====");
        System.out.println("1. 查看所有用户");
        System.out.println("2. 添加新用户");
        System.out.println("3. 删除用户");
        System.out.println("4. 返回上一级");
        System.out.print("请选择[1-4]：");

        int choice = InputUtil.getValidInt();
        switch (choice) {
            case 1:
                userService.listAllUsers();
                break;
            case 2:
                userService.addNewUser();
                break;
            case 3:
                userService.deleteUser();
                break;
            case 4:
                return;
            default:
                System.out.println("❌ 无效选择");
        }
        handleUserManagement(); // 递归显示菜单
    }
}
